<?php

namespace App\Http\Controllers\Home;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends BaseController
{
    //个人中心
    public function show(User $user)
    {
        //文章 关注 粉丝

        $user=User::withCount(['posts','stars','fans'])->find($user->id);

        //获取当前用户文章创建时间前10条
        $posts=$user->posts()->orderBy('created_at','desc')->take(10)->get();

        //这个人关注的用户 包含 关注的用户 粉丝 和 文章数
        $stars=$user->stars;
        $susers=User::whereIn('id',$stars->pluck('star_id'))->withCount(['posts','stars','fans'])->get();

        //这个人粉丝的用户 包含 关注的用户 粉丝 和 文章数
        $fans=$user->fans;
        $fusers=User::whereIn('id',$fans->pluck('fan_id'))->withCount(['posts','stars','fans'])->get();


        return view('user.index',compact('user','posts','susers','fusers'));
    }
    //个人设置
    public function setting()
    {
        
    }
    //个人设置行为
    public function store()
    {
        
    }
    //关注
    public function fan(User $user)
    {
        //当前用户关注其他
        $me=\Auth::user();
        $me->dofan($user->id);
        return [
            'error'=>0,
            'msg'=>'success'
            ];
    }
    //取消关注
    public function unfan(User $user)
    {
        //当前用户关注其他
        $me=\Auth::user();
        $me->unfan($user->id);
        return [
            'error'=>0,
            'msg'=>'success'
        ];
    }
    
}
